﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;

namespace AnyOffice.general.person_info
{
    public partial class myphoto : ModulePage
    {
        private SqlCommand sqlCommand;
        private ConfigManager configManager;
        private string strTemp;
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            this.pnlMain.Visible = false;
            if (base.Conn.State == ConnectionState.Closed)
            {
                base.Conn.Open();
            }
            this.sqlCommand = new SqlCommand("UPDATE Users SET Photo=null where UserId='" + base.User.Identity.Name + "'", base.Conn);
            this.sqlCommand.ExecuteNonQuery();
            this.litMessage.Text = base.ShowPrompt("图片删除成功！", "index.aspx");
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.pnlMain.Visible = false;
            if (this.filePhoto.PostedFile.ContentLength > this.configManager.ImageUploadMaxSize)
            {
                this.litMessage.Text = base.ShowPrompt("照片大小超过500k！", "myphoto.aspx");
            }
            else if (!this.ImageType(this.filePhoto.PostedFile.ContentType))
            {
                this.litMessage.Text = base.ShowPrompt("照片格式不正确！", "myphoto.aspx");
            }
            else if (this.filePhoto.PostedFile.ContentLength > 0)
            {
                int contentLength = this.filePhoto.PostedFile.ContentLength;
                byte[] buffer = new byte[contentLength];
                this.filePhoto.PostedFile.InputStream.Read(buffer, 0, contentLength);
                if (base.Conn.State == ConnectionState.Closed)
                {
                    base.Conn.Open();
                }
                this.sqlCommand = new SqlCommand("UPDATE Users SET Photo = @Photo where UserId='" + base.User.Identity.Name + "'", base.Conn);
                this.sqlCommand.Parameters.Add("@Photo", buffer);
                this.sqlCommand.ExecuteNonQuery();
                this.litMessage.Text = base.ShowPrompt("图片上传成功！", "index.aspx");
            }
        }
        private bool ImageType(string contentType)
        {
            string str;
            return (((str = contentType) != null) && (((str == "image/gif") || (str == "image/jpeg")) || ((str == "image/pjpeg") || (str == "image/bmp"))));
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            this.configManager = new ConfigManager();
            if (!this.Page.IsPostBack)
            {
                this.litImageMaxSize.Text = (this.configManager.ImageUploadMaxSize / 1024).ToString();
                if (base.Conn.State == ConnectionState.Closed)
                {
                    base.Conn.Open();
                }
                this.strTemp = "select Photo from Users where UserId='" + base.User.Identity.Name + "'";
                this.sqlCommand = new SqlCommand(this.strTemp, base.Conn);
                if (this.sqlCommand.ExecuteScalar() != DBNull.Value)
                {
                    this.litPhoto.Text = "<a href=\"../ipanel/photo.aspx?user_id=" + base.User.Identity.Name + "\" target=\"_blank\"><img src=\"../ipanel/photo.aspx?user_id=" + base.User.Identity.Name + "\" border=\"0\" width=\"400px\"></a>";
                    this.litUpload.Text = "照片更改：";
                    this.btnDelete.Visible = true;
                }
            }
        }

    }
}
